//
//  MyEasyTakePicTableViewModel.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2018/12/7.
//  Copyright © 2018年 HCreater. All rights reserved.
//

#import "MyEasyTakePicTableViewModel.h"

#import "SSPPublishContentItemViewModel.h"

#import "SSPsListData.h"

@interface MyEasyTakePicTableViewModel()

@property (nonatomic, readwrite, strong) RACCommand* requestDataCmd;

@property (nonatomic, readwrite, strong) RACCommand* selectCmd;
@property (nonatomic, readwrite, strong) RACCommand* showDetailCmd;

@end

@implementation MyEasyTakePicTableViewModel

- (void)initialize
{
    [super initialize];
    
    @weakify(self);
    
    self.lastPage = 100;
    self.shouldMultiSections = YES;
    /// viewDidLoad后加载数据
    self.shouldRequestRemoteDataOnViewDidLoad = YES;
    /// 允许下拉刷新
    self.shouldPullDownToRefresh = YES;
    /// 允许上拉加载
    self.shouldPullUpToLoadMore = YES;
    
    /// 请求数据
    self.requestDataCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id inputInfo) {
        
        @strongify(self);
        
        NSInteger loadIndex = [inputInfo[@"LoadIndex"] integerValue];
        
        return [self requestListDataSignalWithPageIndex:loadIndex pageSize:List_PageSize];
    }];
    
    self.showDetailCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input)
                          {
                              @strongify(self);
                              
                              NSIndexPath* indexPath = input;
                              
                              SSPPublishContentItemViewModel* vm = self.dataSource[indexPath.section][0];
                              SSPListData* listData = vm.data;
                              
                              return [self requestCircleDetailSignal:listData.circleID];
                          }];
}

- (RACSignal *)requestListDataSignalWithPageIndex:(int)pageIndex pageSize:(int)pageSize
{
    @weakify(self)
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        @strongify(self);
        
        NSInteger reqPage = pageIndex+1;
        NSString* strUserID = (self.reqUserID!=nil && [self.reqUserID isNotBlank]) ? self.reqUserID : @"";
        NSString* strCategory = @"2"; //1随手拍列表 2我的发布
        NSString* strType = @"0";
        NSString* strAreaID = @"";
        
        NSDictionary* postData = interface_param_circleList(@(reqPage), @(pageSize), strUserID, strCategory, strType, strAreaID);
        
        @weakify(self);
        return [[AFNetWorkUtils racPOSTWthURL:url_interface_circleList params:postData] subscribeNext:^(id x)
                {
                    @strongify(self)
                    
                    /// 转化数据
                    @weakify(self);
                    
                    SSPsListData* sspsListData = [SSPsListData modelWithJSON:x];
                    
                    NSArray *viewModels = [sspsListData.datas.rac_sequence map:^NSArray *(SSPListData* data)
                                           {
                                               @strongify(self);
                                               
                                               SSPPublishContentItemViewModel *itemViewModel = [[SSPPublishContentItemViewModel alloc] initWithData:data cellHeight:self.cellHeight];
                                               itemViewModel.isShowDetail = NO;
                                               
                                               return @[itemViewModel];
                                           }].array;
                    
                    if (viewModels == nil || viewModels.count < pageSize)
                    {
                        self.lastPage = self.page;
                        
                        if (viewModels == nil)
                            viewModels = @[];
                    }
                    else
                    {
                        self.lastPage = 999;
                    }
                    
                    if (self.shouldPullUpToLoadMore)
                    {
                        if (0 == pageIndex)
                        {
                            self.dataSource = viewModels;
                        }
                        else
                        {
                            if (self.dataSource == nil)
                            {
                                self.dataSource = viewModels;
                            }
                            else
                            {
                                self.dataSource = [self.dataSource arrayByAddingObjectsFromArray:viewModels];
                            }
                        }
                    }
                    else
                    {
                        self.dataSource = viewModels;
                    }
                    
                    
                    [subscriber sendNext:nil];
                    [subscriber sendCompleted];
                }   error:^(NSError *error) {
                    //错误处理
                    
                    [subscriber sendError:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:error.userInfo[customErrorInfoKey]}]];
                    [subscriber sendCompleted];
                }
                ];
        
        return nil;
    }];
}

- (RACSignal *)requestCircleDetailSignal:(NSString*)circleID
{
    @weakify(self)
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        @strongify(self)
        
        @weakify(self);
        NSDictionary* postData = interface_param_circleDetail(circleID);
        
        [[AFNetWorkUtils racPOSTWthURL:url_interface_circleDetail params:postData] subscribeNext:^(id x)
         {
             @strongify(self)
             
             SSPDetailData* sspDetailData = [SSPDetailData modelWithJSON:x];
             
             [subscriber sendNext:sspDetailData];
             [subscriber sendCompleted];
         }   error:^(NSError *error) {
             //错误处理
             
             [subscriber sendError:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:error.userInfo[customErrorInfoKey]}]];
             [subscriber sendCompleted];
         }];
        
        return nil;
    }];
}

@end

